package com.cssw.fyzb.tps.service.pay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.cssw.fyzb.tps.config.pay.AliPay;
import com.cssw.fyzb.tps.util.AliPayKit;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chenchao
 * @Date: 2020/7/14 22:11
 */

@Service
public class AliPayServiceImpl implements AliPayService {

    private static final Logger logger = LoggerFactory.getLogger(AliPayServiceImpl.class);

    @Autowired
    private AliPay aliPay;

    @Override
    public Map appPay(String outTradeNo, String body, String subject, String amount) throws Exception {
        AliPayApiConfig wxPayApiConfig = aliPay.getApiConfig();
        AliPayApiConfigKit.setThreadLocalAliPayApiConfig(wxPayApiConfig);
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();

        model.setBody(body);//"实名认证费用"
        model.setSubject(subject);//智链视界
        model.setOutTradeNo(outTradeNo);
        model.setTimeoutExpress("30m");
        model.setTotalAmount(amount);

        AlipayTradeAppPayResponse response = AliPayApi.appPayToResponse(model, aliPay.getDomain());
        String responseBody = response.getBody();

        logger.info("ali appPay: {} ", responseBody);

        Map map = new HashMap();
        map.put("tradeNo", outTradeNo);
        map.put("body", responseBody);
        return map;

    }


    @Override
    public Map<String, String> orderQuery(String tradeNo) throws Exception {
        AliPayApiConfig wxPayApiConfig = aliPay.getApiConfig();
        AliPayApiConfigKit.setThreadLocalAliPayApiConfig(wxPayApiConfig);

        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        AlipayTradeQueryResponse response = AliPayApi.tradeQueryToResponse(model);
        logger.info("ali orderQuery: {} ", response.getBody());

        if (!response.isSuccess()) {
            logger.error("ali orderQuery: {} ", response.getBody());
            return null;
        }

        Map dataMap = AliPayKit.getResponseParam(response);
        //交易状态：WAIT_BUYER_PAY（交易创建，等待买家付款）、TRADE_CLOSED（未付款交易超时关闭，或支付完成后全额退款）、TRADE_SUCCESS（交易支付成功）、TRADE_FINISHED（交易结束，不可退款）
        dataMap.put("tradeStatus", response.getTradeStatus());
        return dataMap;
    }

    @Override
    public boolean certNotifyUrl(Map<String, String> params) throws Exception {
        logger.info("ali certNotifyUrl: {} ", JSON.toJSONString(params));


        AliPayApiConfig wxPayApiConfig = aliPay.getApiConfig();
        AliPayApiConfigKit.setThreadLocalAliPayApiConfig(wxPayApiConfig);
        return AlipaySignature.rsaCertCheckV1(params, wxPayApiConfig.getAliPayCertPath(), "UTF-8", "RSA2");

    }


}
